package com.xperblueray.stepbystep.mail.impl;

import com.xperblueray.stepbystep.mail.SendJunkMailService;
import com.xperblueray.stepbystep.model.SysDict;
import com.xperblueray.stepbystep.service.SysDictService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
public class SendJunkMailServiceImpl implements SendJunkMailService {
    Logger logger = LogManager.getLogger(this.getClass());

    @Resource
    SysDictService sysDictService;

    @Autowired
    JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    String from;

    @Override
    public boolean sendJunkMail(List<SysDict> infos) {
    try {
        if(CollectionUtils.isEmpty(infos)) {
            return Boolean.FALSE;
        }

        for(SysDict info : infos) {
            MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            // 邮件发送方
            message.setFrom(from);
            // 邮件主题
            message.setSubject("苹果今天特卖");
            // 邮件接收方
            message.setTo("xperblueray@outlook.com");
            // 邮件内容
            message.setText("你知道吗？今天苹果特卖，一斤只要3元");
            // 发送邮件
            logger.info("发送邮件");
//            this.javaMailSender.send(mimeMessage);
        }
    } catch (MessagingException e) {
        logger.error("sendJunkMail error!", e);
        return Boolean.FALSE;
    } catch (Exception e) {
        logger.error("sendJunkMail error!", e);
        return Boolean.FALSE;
    }
    return Boolean.TRUE;


    }
}
